//
//  MGTableViewManage.m
//  Express
//
//  Created by lhongl on 2025/9/25.
//  Copyright © 2025 QM. All rights reserved.
//

#import "MGTableViewManage.h"
#import "MGSection.h"
#import "MGItem.h"
@interface MGTableViewManage () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray <MGSection *> *dataArray;

@end

@implementation MGTableViewManage
- (instancetype)initTableViewManageWithTableView:(__kindof UITableView *)tableView {
    if (self = [super init]) {
        if (tableView) {
            _tableView = tableView;
            tableView.delegate = self;
            tableView.dataSource = self;
        }
    }
    return self;
}

- (NSArray *)sections{
    return _dataArray;
}

#pragma mark 注册
- (void)registerCellWitNibName:(NSString *)nibName{
     NSAssert(nibName, @"[MGTableView_DEBUG] nibName can not nil");
    [self.tableView registerNib:[UINib nibWithNibName:nibName bundle:nil] forCellReuseIdentifier:nibName];
}

- (void)registerCellWitClass:(Class)cellClass{
    NSAssert(cellClass, @"[MGTableView_DEBUG] cellClass can not nil");
    NSString *identifier = NSStringFromClass(cellClass);
    [self.tableView registerClass:cellClass forCellReuseIdentifier:identifier];
}
- (void)registerHeaderFooterWitNibName:(NSString *)nibName{
    NSAssert(nibName, @"[MGTableView_DEBUG] nibName can not nil");
    [self.tableView registerNib:[UINib nibWithNibName:nibName bundle:nil] forHeaderFooterViewReuseIdentifier:nibName];
}
- (void)registerHeaderFooterWitClass:(Class)aClass{
    NSAssert(aClass, @"[MGTableView_DEBUG] aClass can not nil");
    NSString *identifier = NSStringFromClass(aClass);
    [self.tableView registerClass:aClass forHeaderFooterViewReuseIdentifier:identifier];
}

#pragma mark - 添加数据源
- (void)addSection:(MGSection *)section {
    if (!section) {
#ifdef DEBUG
        NSAssert(section, @"[MGTableView_DEBUG] section Not Null");
#endif
        return;
    }
    section.tableManage = self;
    [self.dataArray addObject:section];
}

- (void)addSections:(NSArray <MGSection *> *)sections {
    if (!sections || sections == NULL || sections.count == 0) {
        return;
    }
    for (MGSection *section in sections){
        section.tableManage = self;
    }
    [self.dataArray addObjectsFromArray:sections];
}
- (void)addSection:(MGSection *)section atIndex:(NSInteger)index {
    if (!section) {
#ifdef DEBUG
        NSAssert(section, @"[MGTableView_DEBUG] section Not Null");
#endif
        return;
    }

    if (self.dataArray.count < index) {
#ifdef DEBUG
        NSString *des = [NSString stringWithFormat:@"[MGTableView_DEBUG]: index %ld beyond bounds [0 .. %ld]",index,self.dataArray.count];
        NSAssert(NO, des);
#endif
        return;
    }
    section.tableManage = self;
    [self.dataArray insertObject:section atIndex:index];
}

#pragma mark 删除
- (void)removeSectionAtIndex:(NSInteger)index {
    if (self.dataArray.count <= index) {
#ifdef DEBUG
        NSString *des = [NSString stringWithFormat:@"[MGTableView_DEBUG]: index %ld beyond bounds [0 .. %ld]",index,self.dataArray.count - 1];
        NSAssert(NO, des);
#endif
        return;
    }
    [self.dataArray removeObjectAtIndex:index];
}

- (void)removeSectionsWithSections:(NSArray <MGSection *>*)sections {
    if (sections == NULL || sections.count == 0) {
        return;
    }
    [self.dataArray removeObjectsInArray:sections];
}

- (void)removeAllSections {
    [self.dataArray removeAllObjects];
}
- (void)removeSection:(MGSection *)section {
    if (!section) {
#ifdef DEBUG
        NSAssert(section, @"[MGTableView_DEBUG] section Not Null");
#endif
        return;
    }
    [self.dataArray removeObject:section];
}

#pragma mark 修改
- (void)replaceSectionWithIndex:(NSUInteger)index section:(MGSection *)section{
    if (!section) {
#ifdef DEBUG
        NSAssert(section, @"[MGTableView_DEBUG] section Not Null");
#endif
        return;
    }
    if (self.dataArray.count <= index) {
#ifdef DEBUG
        NSString *des = [NSString stringWithFormat:@"[MGTableView_DEBUG]: index %ld beyond bounds [0 .. %ld]",index,self.dataArray.count - 1];
        NSAssert(NO, des);
#endif
        return;
    }
    section.tableManage = self;
    [self.dataArray replaceObjectAtIndex:index withObject:section];
}

#pragma mark - 刷新
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation API_AVAILABLE(ios(3.0)){
    [self reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];
}
- (void)reloadData {
    [self.tableView reloadData];
}

#pragma mark 代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataArray.count == 0) {
        return 0;
    }
    MGSection *dataSection = self.dataArray[section];
    return dataSection.itemList.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.dataArray.count;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArray.count == 0) {
        return;
    }
    if (self.dataArray.count <= indexPath.section) {
        return;
    }
    MGSection *dataSection = self.dataArray[indexPath.section];
    if (dataSection.itemList.count <= indexPath.row) {
        return;
    }
    MGItem *item  = dataSection.itemList[indexPath.row];
    if (item.willDisplayCell) {
       item.willDisplayCell(self, indexPath, cell, item);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MGSection *dataSection = self.dataArray[indexPath.section];
    MGItem *item  = dataSection.itemList[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:item.identifier forIndexPath:indexPath];
    [item tableViewManage:self cell:cell];
    if (item.cellConfiguration) {
        item.cellConfiguration(self, indexPath, cell, item);
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataArray.count == 0) {
        return 0;
    }
    MGSection *dataSection = self.dataArray[indexPath.section];
    MGItem *item  = dataSection.itemList[indexPath.row];
    return item.rowHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (self.dataArray.count == 0) {
#ifdef DEBUG
        NSAssert(NO, @"dataArray not nil");
#endif
        return;
    }
    MGSection *dataSection = self.dataArray[indexPath.section];
    MGItem *item  = dataSection.itemList[indexPath.row];
    if (item.didSelectRow) {
        item.didSelectRow(self, indexPath, item);
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    MGSection *dataSection = self.dataArray[indexPath.section];
    MGItem *item  = dataSection.itemList[indexPath.row];
    if (item.didDeselectRow) {
        item.didDeselectRow(self, indexPath, item);
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.dataArray.count == 0) {
        return nil;
    }
    MGSection *dataSection = self.dataArray[section];
    if (dataSection.customViewForHeader) {
        return dataSection.customViewForHeader(section);
    }

    if (dataSection.headerIdentifier.length == 0) {
        return [UIView new];
    }
    UIView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:dataSection.headerIdentifier];
    if (headerView == nil) {
        headerView = [UIView new];
    }
    if (dataSection.viewForHeader) {
        dataSection.viewForHeader(headerView, section);
    }
    return headerView;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (self.dataArray.count == 0) {
        return nil;
    }
    MGSection *dataSection = self.dataArray[section];
    if (dataSection.titleForHeader) {
        return dataSection.titleForHeader(section);
    }
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.dataArray.count == 0) {
        return CGFLOAT_MIN;
    }
    MGSection *dataSection = self.dataArray[section];
    if (dataSection.heightForHeader) {
        return dataSection.heightForHeader(section);
    }
    return CGFLOAT_MIN;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (self.dataArray.count == 0) {
        return nil;
    }

    MGSection *dataSection = self.dataArray[section];
    if (dataSection.customViewForFooter) {
        return dataSection.customViewForFooter(section);
    }

    if (dataSection.footerIdentifier.length == 0) {
        return [UIView new];
    }
    UIView *footererView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:dataSection.footerIdentifier];
    if (footererView == nil) {
        footererView = [UIView new];
    }
    if (dataSection.viewForFooter) {
        dataSection.viewForFooter(footererView, section);
    }
    return footererView;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    if (self.dataArray.count == 0) {
        return nil;
    }

    MGSection *dataSection = self.dataArray[section];
    if (dataSection.titleForFooter) {
        return dataSection.titleForFooter(section);
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.dataArray.count == 0) {
        return CGFLOAT_MIN;
    }
    MGSection *dataSection = self.dataArray[section];
    if (dataSection.heightForFooter) {
        return dataSection.heightForFooter(section);
    }
    return CGFLOAT_MIN;
}

- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    MGSection *dataSection = self.dataArray[indexPath.section];
    MGItem *item  = dataSection.itemList[indexPath.row];
    if (item.editActions) {
        return item.editActions(self,indexPath,item);
    }
    return nil;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    MGSection *dataSection = self.dataArray[indexPath.section];
    MGItem *item  = dataSection.itemList[indexPath.row];
    if (item.editActions) {
        return YES;
    }
    return NO;
}


#pragma mark 懒加载
- (NSMutableArray <MGSection *> *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
